<?php
// +----------------------------------------------------------------------
// | 中联致远服务端框架
// +----------------------------------------------------------------------
// | Copyright (c) 2018 http://www.zhongliansoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: tony <tanyu@zhongliansoft.com>
// +----------------------------------------------------------------------
namespace app\Manage\controller;

use app\common\controller\Manage;
use app\common\model\Recharge as RechargeModel;
use think\facade\Request;
use think\facade\Log;
use think\Db;

class Recharge extends Manage
{

    /**
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index()
    {
        if(Request::isAjax())
        {
            $RechargeModel = new RechargeModel();
            return  $RechargeModel->tableData(input('param.'));
        }
        return $this->fetch();
    }

    /**
     *  充值添加信息
     * User:tony
     * @return array|mixed
     */
    public function add()
    {
        $RechargeModel = new RechargeModel();
        if(Request::isPost())
        {
            return $RechargeModel->addData(input('param.'));
        }
        return $this->fetch();
    }

    /**
     *
     *  充值信息编辑
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function edit()
    {
        $RechargeModel = new RechargeModel();

        $sql = "select * from zl_bill_payments a, zl_bill_payments_rel b where a.payment_id = b.payment_id and a.type = 2 and b.param = '".input('param.id/d')."'";
    
        $list = Db::query($sql); 
        if (!$list || count($list) == 0) { 
            if(Request::isPost())
            {
                return $RechargeModel->saveData(input('param.'));
            }
            $RechargeModel = new RechargeModel();
            $info = $RechargeModel->where('id',input('param.id/d'))->find();

            $info['ctime'] = date('Y-m-d H:i:s', $info['ctime']);
            $info['utime'] = date('Y-m-d H:i:s', $info['utime']);

            if($info['expires_in'] == '') {
                $info['expires_in'] = '';
            } else {
                $info['expires_in'] = date('Y-m-d', $info['expires_in']);
            }

            if (!$info)
            {
                return error_code(10002);
            }
            return $this->fetch('edit',[
                'info'=>$info
            ]);
        } else { 
            $this->error('当前充值规则已有用户充值无法编辑！');
        }
    }

    /**
     *
     * 充值信息删除
     * User:tony
     * @return array
     */
    public function del()
    {
        $RechargeModel = new RechargeModel();
        $result = ['status'=>false,'msg'=>'删除失败','data'=>''];

        $sql = "select * from zl_bill_payments a, zl_bill_payments_rel b where a.payment_id = b.payment_id and a.type = 2 and b.param = '".input('param.id/d')."'";
    
        $list = Db::query($sql); 
        if (!$list || count($list) == 0) { 
            if ($RechargeModel->where('id',input('param.id/d'))->delete())
            {
                $result['status'] = true;
                $result['msg'] = '删除成功';
            }
        } else { 
            $result['msg']    = '当前充值规则已有用户充值无法删除！';
        }

        return $result;
    }

    public function changeStatus()
    {
        $RechargeModel = new RechargeModel();
        return $RechargeModel->changeStatus(input('param.id/d'),input('param.is_pub'));
    }
    
}
